package org.xinjiang.tarim.travel.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.xinjiang.tarim.travel.mapper.TopicMapper;
import org.xinjiang.tarim.travel.model.Topic;
import org.xinjiang.tarim.travel.model.TopicImpression;
import org.xinjiang.tarim.travel.service.ImpressionService;
import org.xinjiang.tarim.travel.service.TopicImpressionService;
import org.xinjiang.tarim.travel.service.TopicService;
import org.xinjiang.tarim.travel.vo.Criteria;
import org.xinjiang.tarim.travel.vo.PageBean;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class TopicServiceImpl implements TopicService {
    @Autowired
    TopicMapper topicMapper;
    @Autowired
    TopicImpressionService topicImpressionService;
    @Autowired
    ImpressionService impressionService;

    @Override
    public List<Topic> insertByList(List<Topic> topics) {
        topicMapper.insertByList(topics);
        return topics;
    }

    @Override
    public Long insert(Topic topic) {
        topicMapper.insert(topic);
        return topic.getId();
    }

    @Override
    public PageBean selectByCondition(Criteria criteria, PageBean pageBean) {
        criteria.addExtField("pageIndex", (pageBean.getPageIndex()-1) * pageBean.getPageSize());
        criteria.addExtField("pageSize", pageBean.getPageSize());
        Long totalCount = topicMapper.selectByConditionCount(criteria);
        List<Topic> resultList = topicMapper.selectByCondition(criteria);
        List<Map> list1 = new ArrayList<>();
        for (Topic topic : resultList){
            List<TopicImpression> list = topicImpressionService.selectByTopicId(topic.getId());
            List<Map> mapList = impressionService.selectByPrimaryKeys(list);
            Map map = new HashMap();
            map.put("topic", topic);
            map.put("impression", mapList);
            list1.add(map);
        }
        pageBean.setTotalCount(totalCount);
        pageBean.setResultList(list1);
        return pageBean;
    }

    @Override
    public List selectByPrimaryKeys(List list) {
        return topicMapper.selectByPrimaryKeys(list);
    }

    @Override
    public Topic selectByPrimaryKey(Long id) {
        return topicMapper.selectByPrimaryKey(id);
    }
}
